package com.lezhu.plcsdklibrary.manager;

import com.lezhu.plcsdklibrary.bean.CallBackBean;
import com.lezhu.plcsdklibrary.callback.DoorAllOpenStatusInterface;
import com.lezhu.plcsdklibrary.callback.DoorStatusInterface;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述：状态回调管理
 * 作者: 钮家齐
 * 时间: 2020/5/28 10:01
 */
public class DoorCallBackManager {

    private static DoorCallBackManager doorCallBackManager;
    private static HashMap<Integer, DoorStatusInterface> map;
    private static ArrayList<Integer> list;

    private DoorAllOpenStatusInterface doorAllOpenStatusInterface;

    public static DoorCallBackManager getInstance(){
        if(doorCallBackManager==null){
            synchronized (DoorCallBackManager.class){
                if(doorCallBackManager==null){
                    doorCallBackManager = new DoorCallBackManager();
                    map = new HashMap<>();
                    list = new ArrayList<>();
                }
            }
        }
        return doorCallBackManager;
    }

    public void addCallBack(int pos, DoorStatusInterface statusInterface){
        list.add(pos);
        map.put(pos,statusInterface);
    }

    public CallBackBean getCallBack(){
        if(list.size()!=0){
            Integer pos = list.get(0);
            if(map.containsKey(pos)){
                list.remove(pos);
                DoorStatusInterface doorStatusInterface = map.get(pos);
                map.remove(pos);
                CallBackBean callBackBean = new CallBackBean();
                callBackBean.pos = pos;
                callBackBean.statusInterface = doorStatusInterface;
                return callBackBean;
            }
        }
       return null;
    }

    public void setAllOpenStatusInterface(DoorAllOpenStatusInterface statusInterface) {
        doorAllOpenStatusInterface = statusInterface;
    }

    public DoorAllOpenStatusInterface getDoorAllOpenStatusInterface() {
        return doorAllOpenStatusInterface;
    }
}
